package com.itheima.多线程.基础;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class lockSy implements Runnable {
    private static long count = 100;
    Lock lock=new ReentrantLock();
    @Override
    public void run() {
        while (true) {
            try {
                lock.lock();
                if (count > 0) {
                    Thread.sleep(20);
                    count--;
                    System.out.println(Thread.currentThread().getName() + "在卖票,还剩" + count);
                } else {
                    break;
                }
                lock.unlock();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
            }
        }
    }
}
 class Demo22 {
    public static void main(String[] args) {
        lockSy ticket = new lockSy();

        Thread t1 = new Thread(ticket);
        Thread t2 = new Thread(ticket);
        Thread t3 = new Thread(ticket);

        t1.setName("窗口一");
        t2.setName("窗口二");
        t3.setName("窗口三");

        t1.start();
        t2.start();
        t3.start();
    }
}